// pages/all/all.js

var api = require('../../utils/api.js');
import { checkAndFetchToken, refreshToken } from '../../utils/Logic.js';
import net from '../../utils/Request.js';
import util from '../../utils/util.js';

const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        allOrders: [],
        currentDate: '2018-01-01'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        this.setData({
            currentDate: util.formatDate(new Date())
        });
        console.log(util.formatDate(new Date()));
        checkAndFetchToken(this.route, this.fetchData);
    },

    fetchData: function () {

        var that = this;
        wx.showNavigationBarLoading();

        net.post(api.baseUrl + '/api/getOneDayAllOrders', {
            ...app.globalData.access_token,
            lunchDate: that.data.currentDate
        }).then(res => res.data).then(res => {
            console.log('success all ', res);
            if (res.status === 20000) {
                that.setData({
                    allOrders: res.data
                });
            }
        }).catch(e => {
            if (e.statusCode === 401) {
                refreshToken(that.route);
            }
            console.error('all page error ', e);
        }).then(res => {
            wx.hideNavigationBarLoading();
            wx.stopPullDownRefresh();
        });
    },
    bindDateChange: function (e) {
        console.log(e.detail.value);
        this.setData({
            currentDate: e.detail.value
        });
        this.fetchData();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.showNavigationBarLoading();
        this.fetchData();
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    onPullDownRefresh: function () {
        this.fetchData();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})